package com.newer.his.Enjoin.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.newer.his.common.entity.Dept;
import com.newer.his.common.entity.Doctor;
import com.newer.his.common.entity.Enjoin;
import com.newer.his.common.entity.Patient;

//医嘱对象字段
//int id;			//主键
//Doctor doctor;	//医生对象
//Dept dept;		//科室对象
//Patient patien;	//患者对象
//String status;	//医嘱状态
//Date enjonTime;	//开医嘱时间
//Date payTime;	// 缴费时间
//Date getTime;	// 领药时间
//String isrun;	// 护士站是否执行
//String isblood;	// 护士站打印抽血条码

//+-----------+-----------+------------+---------+--------+------------+---------+---------+--------+---------+
//| enjoin_id | id_doctor | id_patient | id_dept | status | enjoinTime | payTime | getTime | isrun  | isblood |
//+-----------+-----------+------------+---------+--------+------------+---------+---------+--------+---------+
//|         1 |         4 |          2 |       3 | 待领药 | NULL       | NULL    | NULL    | 未执行 | 未打印  |
//|         2 |         5 |          3 |       3 | 待领药 | NULL       | NULL    | NULL    | 未执行 | 未打印  |
//|        10 |         4 |          2 |       3 | 待收费 | NULL       | NULL    | NULL    | 未执行 | 未打印  |
//+-----------+-----------+------------+---------+--------+------------+---------+---------+--------+---------+

@Mapper
public interface EnjoinMapper {

	// 创建医嘱
	@Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
	@Insert("insert into enjoin(`id_doctor`,`id_patient`,`id_dept`,`enjoinTime`)values(#{doctor.id},#{patient.id},#{dept.id},now());")
	void save(Enjoin e);
	
	// 查特定医嘱对象
	@Select("select enjoin_id, id_doctor ,id_patient,id_dept,`status` ,enjoinTime, payTime ,getTime  from enjoin where enjoin_id=#{id};")
	@Results(
				id="",
				value = {
						@Result(column="enjoin_id",property = "id"),
						@Result(
								column="id_doctor",
								property="doctor",
								javaType = Doctor.class,
								one=@One(select="com.newer.his.common.mapper.DoctorMapper.Did")
								),
						@Result(
								column = "id_patient",
								property = "patient",
								javaType = Patient.class,
								one=@One(select="com.newer.his.common.mapper.PatientMapper.findByid")
								),
						@Result(
								column = "id_dept",
								property = "dept",
								javaType = Dept.class,
								one=@One(select="com.newer.his.common.mapper.DeptMapper.Byid")),
						@Result(column = "status",property = "status"),
						@Result(column = "enjoinTime",property = "enjoinTime"),
						@Result(column = "payTime",property = "payTime"),
						@Result(column = "getTime",property = "getTime"),
						@Result(column = "isrun",property = "isrun"),
						@Result(column = "isblood",property = "isblood")
				}
			)
	Enjoin EnjoinfindByid(int id);
	
	// 查所有医嘱
	@Select("SELECT * FROM enjoin;")
	@Results(
			id="",
			value = {
				@Result(column="enjoin_id",property = "id"),
				@Result(
						column="id_doctor",
						property="doctor",
						javaType = Doctor.class,
						one=@One(select="com.newer.his.common.mapper.DoctorMapper.Did")
						),
				@Result(
						column = "id_patient",
						property = "patient",
						javaType = Patient.class,
						one=@One(select="com.newer.his.common.mapper.PatientMapper.findByid")
						),
				@Result(
						column = "id_dept",
						property = "dept",
						javaType = Dept.class,
						one=@One(select="com.newer.his.common.mapper.DeptMapper.Byid")),
				@Result(column = "status",property = "status"),
				@Result(column = "enjoinTime",property = "enjoinTime"),
				@Result(column = "payTime",property = "payTime"),
				@Result(column = "getTime",property = "getTime"),
				@Result(column = "isrun",property = "isrun"),
				@Result(column = "isblood",property = "isblood")
			}
			)
	List<Enjoin> FindAll();
	
	// 修改医嘱
	@Update("update enjoin set id_doctor=#{doctor.id},"
			+ "id_patient=#{patient.id},id_dept=#{dept.id},"
			+ "`status`=#{status},enjoinTime=#{enjonTime} where enjoin_id=#{id};")
	void update(Enjoin enjoin);
	
	// 通过id删除医嘱
	@Delete("delete from enjoin where enjoin_id=#{id};")
	void delete(int id);


	

	

}
